package com.atguigu.gmall.cart.service.impl;

import com.atguigu.gmall.cart.mapper.CartInfoMapper;
import com.atguigu.gmall.cart.service.CartAsyncService;
import com.atguigu.gmall.model.cart.CarInfoVo;
import com.atguigu.gmall.model.cart.CartInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @author: JC
 * @date: 2021/11/3 18:42
 */
@Service
public class CartAsyncServiceImpl extends ServiceImpl<CartInfoMapper, CartInfo> implements CartAsyncService {

    @Autowired
    private CartInfoMapper cartInfoMapper;

    @Async
    @Override
    public void saveCartInfo(CartInfo cartInfo) {
        save(cartInfo);
    }

    @Async
    @Override
    public void updateCartInfo(CartInfo cartInfo) {
        update(
                cartInfo, new QueryWrapper<CartInfo>()
                .eq("user_id", cartInfo.getUserId())
                .eq("sku_id", cartInfo.getSkuId())
        );
    }

    @Override
    @Async
    public void deleteCartInfo(CartInfo cartInfo) {
        cartInfoMapper.delete(new QueryWrapper<CartInfo>().eq("user_id",cartInfo.getUserId()));
    }

    @Override
    @Async
    public void checkCart(String userId, Integer isChecked, Long skuId) {
        QueryWrapper<CartInfo> cartInfoQueryWrapper = new QueryWrapper<>();
        cartInfoQueryWrapper.eq("user_id",userId).eq("sku_id",skuId);

        CartInfo cartInfo = new CartInfo();
        cartInfo.setIsChecked(isChecked);

        update(cartInfo, cartInfoQueryWrapper);

    }

    @Async
    public void deleteCartInfo(String userId, Long skuId) {
        QueryWrapper<CartInfo> cartInfoQueryWrapper = new QueryWrapper<>();
        cartInfoQueryWrapper.eq("user_id",userId).eq("sku_id",skuId);
        cartInfoMapper.delete(cartInfoQueryWrapper);
    }
}
